package com.study.collection;


import org.openjdk.jol.info.ClassLayout;

import java.util.stream.IntStream;

public class YesLockTest {

    static Object yesLock;

    volatile static String s;
    public static void main(String[] args) throws InterruptedException {
        yesLock = new Object();
        System.out.println("无锁时对象布局：" + ClassLayout.parseInstance(yesLock).toPrintable());
        IntStream.rangeClosed(1,4).forEach(i->{getYesLock();});
        Thread.sleep(5000L);
        System.out.println("无竞争之后，此时的对象布局：" + ClassLayout.parseInstance(yesLock).toPrintable());
        getYesLock();//此时再来一次加锁

        s= "hello";
        new Thread(()->{
            synchronized (s){
                while (true){
                    try{
                        System.out.println(s);
                        Thread.sleep(1000);
                    }catch (Exception e){

                    }
                }
            }
        }).start();

        new Thread(()->{
            try{
                Thread.sleep(2000);
            }catch (Exception e){

            }
           s = "yes";
        }).start();
    }


    private static void getYesLock() {
        new Thread(() -> {
            try {
                synchronized (yesLock) {
                    System.out.println("线程[" + Thread.currentThread().getName() + "]" +
                            ":重量级锁状态对象布局:" + ClassLayout.parseInstance(yesLock).toPrintable());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

}
